package com.yisin.www.chat;

import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;

import com.yisin.pbox.frame.util.DateUtil;
import com.yisin.pbox.frame.util.StringUtil;

public class RobotThread implements Runnable {
	
	public void run() {
		int rs = 0;
		while (true) {
			try {
				Date day = new Date();
				int hour = day.getHours();
				if (hour > 7 || hour < 2) {
					send();
				}
				int len = (int) (Math.random() * 20) + 1;
				Thread.sleep(1000 * len + 1000);
				rs++;
				if(rs > 3){
					rs = 0;
					addOrDelRobot();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void send() {
		Object[] ips = ChatRobot.userMap.keySet().toArray();
		if(ips == null || ips.length == 0){
			return;
		}
		String fromuser = StringUtil.objectToString(ips[(int) (Math.random() * ips.length)]);
		String touser = "all_user";
		String msg = ChatRobot.msgList.get((int) (Math.random() * ChatRobot.msgList.size()));
		Map<String, String> fUser = ChatRobot.userMap.get(fromuser);
		Map<String, Object> msgMap = new HashMap<String, Object>();
		msgMap.put("fromName", fUser.get("name"));
		msgMap.put("fromId", fUser.get("userId"));
		msgMap.put("msg", msg);
		msgMap.put("time", DateUtil.format(new Date()));
		msgMap.put("genre", 3);// 聊天
		if (touser.equals("all_user")) { // 群聊
			String tUserId = null, iusid = "";
			for (Socket tosoc : ChatServlet.socketMap.values()) {
				if (tosoc != null) {
					iusid = ChatServlet.getUserId(tosoc);
					if (iusid != null) {
						tUserId = iusid;
						msgMap.put("touser", tUserId);
						msgMap.put("flag", 1);// 群聊
						ChatServlet.getChat().sendMsgToClient(tosoc, ChatServlet.getChat().mapToJsonString(msgMap));
					}
				}
			}
			
			int r = (int)(Math.random() * 20);
			if(r < 2){
				JSONObject msgObj = new JSONObject();
				msgObj.put("content", msg);
				ChatServlet.cr.checkAndRe(null, fromuser, null, msgObj, 5000);
			}
		}
	}
	
	
	public void addOrDelRobot(){
		int r = (int)(Math.random() * 10) + 1;
		if(r%2 == 0){ // --
			if(ChatRobot.ipUser.size() > 30){
				Set<String> set = ChatRobot.ipUser.keySet();
				Iterator ite = set.iterator();
				if(ite.hasNext()){
					String id = (String) ite.next();
					String key = ChatRobot.ipUser.get(id);
					ChatRobot.ipUser.remove(id);
					ChatRobot.names.remove(id);
					ChatRobot.userMap.remove(key);
					
					//ChatServlet.ipUser.remove(id);
					//ChatServlet.userMap.remove(key);
					ChatServlet.getChat().pushUserLogout(key);
					System.out.println("删除机器人：" + key);
				}
			}
		} else { // ++
			if(ChatRobot.ipUser.size() < ChatRobot.nameArray.length){
				ChatRobot cr = new ChatRobot();
				Map<String, String> userInfo = cr.createRobot(1);
				
				ChatServlet.getChat().pushUserLogin(null, userInfo);
				System.out.println("增加机器人：" + userInfo.get("ip"));
			}
		}
	}
	
}
